package com.collins.io.practice;

import org.apache.log4j.Logger;

import java.io.*;
import java.util.*;

public class CopyDir {
    private static Logger logger = Logger.getLogger(CopyDir.class.getName());

    public static void main(String[] args) {
        CopyDir c = new CopyDir();
        c.copyDir("files/ddd", "files/eee");

    }

    public void copyDir(String srcDir, String destDir) {
        File src = new File(srcDir);
        File dest = new File(destDir);
        dest.mkdirs();
        File[] files = src.listFiles();
        assert files != null;
        for (File file : files) {
            if (!file.isDirectory()) {
                copyFile(file, new File(destDir + File.separator + file.getName()));
            } else {
                copyDir(file.getAbsolutePath(), destDir + File.separator + file.getName());
            }
        }
    }

    public void copyFile(File src, File dest) {
        try {
            FileInputStream fis = new FileInputStream(src);
            FileOutputStream fos = new FileOutputStream(dest);
            int len;
            byte[] buffer = new byte[1024];
            while ((len = fis.read()) != -1) {
                fos.write(buffer, 0, len);
            }
            fis.close();
            fos.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void readAndSort(String srcDir, String dstDir) {
        try {
            // read file
            FileInputStream fis = new FileInputStream(new File(srcDir));
            byte[] buffer = new byte[1024];
            int len = fis.read(buffer);
            String str = new String(buffer, 0, len);
            logger.info("original num series: " + str);
            // String -> String[] -> int[] -> Arrays.sort()
            String[] split = str.split("-");
            int[] nums = new int[split.length];
            for (int i = 0; i < split.length; i++) {
                nums[i] = Integer.parseInt(split[i]);
            }
            Arrays.sort(nums);
            logger.info(Arrays.toString(nums));
            // int[] -> String
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < nums.length; i++) {
                if (i == nums.length - 1) {
                    sb.append(nums[i]);
                } else {
                    sb.append(nums[i]);
                    sb.append("-");
                }
            }
            // write file
            logger.info("sorted num series: " + sb.toString());
            FileOutputStream fos = new FileOutputStream(new File(dstDir));
            fos.write(sb.toString().getBytes());
            //
            fos.close();
            fis.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void numberedTextSort(String srcDir, String dstDir) {
        try {
            // read from files
            BufferedReader br = new BufferedReader(new FileReader(new File(srcDir)));
            List<String> strs = new ArrayList<>();
            String line;
            while((line = br.readLine())!=null){
                strs.add(line);
            }
            br.close();
            logger.info("original num series: " + strs);
            // sort
            Collections.sort(strs, new Comparator<String>() {
                @Override
                public int compare(String o1, String o2) {
                    int i1 = Integer.parseInt(o1.split("\\.\\s")[0]);
                    int i2 = Integer.parseInt(o2.split("\\.\\s")[0]);
                    return i1 - i2;
                }
            });
            logger.info("sorted num series: " + strs);
            // writer to file
            BufferedWriter bw = new BufferedWriter(new FileWriter(new File(dstDir)));
            for (String str : strs) {
                bw.write(str);
                bw.newLine();
            }
            bw.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
